package com.ntlg.shirospringboot.config;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {
    /*subject*//*ShiroFilterFactoryBean*/
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(@Qualifier("defaultWebSecurityManager") DefaultWebSecurityManager defaultWebSecurityManager ){
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(defaultWebSecurityManager);

        /*进行*/
        Map<String,String> filterMap = new LinkedHashMap<>();
        filterMap.put("/level1/1","anon");
        filterMap.put("/level1/2","authc");
        filterMap.put("/level1/3","perms[shiro:update]");
        filterMap.put("/level2/1","anon");
        filterMap.put("/level2/2","authc");
        filterMap.put("/level2/3","perms[shiro:add]");
        filterMap.put("/level3/1","anon");
        filterMap.put("/level3/2","authc");
        filterMap.put("/level3/3","perms[shiro:add]");

        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterMap);
        /*没有认证时进入登录页面*/
        shiroFilterFactoryBean.setLoginUrl("/toLogin");
        /*没有授权时进入的页面*/
        shiroFilterFactoryBean.setUnauthorizedUrl("/unauthorize");
        return shiroFilterFactoryBean;
    }

    /*SecurityManager*//*DefaultWebSecurityManager*/
    @Bean
    public DefaultWebSecurityManager defaultWebSecurityManager(@Qualifier("myRealm") MyRealm myRealm){
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
        defaultWebSecurityManager.setRealm(myRealm);
        return defaultWebSecurityManager;
    }
    /*realm*//*MyRealm*/

    @Bean
    public MyRealm myRealm(){
        return new MyRealm();
    }

    @Bean
    public ShiroDialect getShiroDialect() {
        return new ShiroDialect();
    }

    }
